package com.mask.core.jukebox;

import java.awt.print.Book;
import java.io.*;
import java.util.*;

/**
 * ClassName JukeBox
 * Create by hx
 * Date 2021/9/3 2:58 下午
 */

public class JukeBox {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {

        ArrayList<BookEntity> songList = new ArrayList<>();
        getBooks(songList);
        while (true){
            System.out.println("图书排序：书名(1),作者(2),价格(3),退出(0)");
            int code = scanner.nextInt();
            List<BookEntity> sortList = sort(songList, code);
            printBookList(sortList);
            if (code == 0){
                break;
            }
        }
    }

    /**
     * 打印图书信息
     * @param bookList
     */
    public static void printBookList(List<BookEntity> bookList){
        for (int i = 0; i < bookList.size(); i++) {
            System.out.println("图书信息"+(i+1)+":"+bookList.get(i));
        }
        System.out.println("====================");
    }

    /**
     * 图书按照条件排序
     * @param list
     * @param code
     * @return
     */
    private static List<BookEntity> sort(List<BookEntity> list,int code){
        switch (code){
            case 1:
                System.out.println("图书信息按照书名排序");
                System.out.println("====================");
                list.sort((BookEntity b1,BookEntity b2) -> b1.getBookName().compareTo(b2.getBookName()));
                break;
            case 2:
                System.out.println("图书信息按照作者排序");
                System.out.println("====================");
                list.sort((BookEntity b1,BookEntity b2) -> b1.getAuthor().compareTo(b2.getAuthor()));
                break;
            case 3:
                System.out.println("图书信息按照价格排序");
                System.out.println("====================");
                list.sort((BookEntity b1,BookEntity b2) -> b1.getPrice().compareTo(b2.getPrice()));
                break;
        }
        return list;
    }

    /**
     * 获取图书信息
     * @param songList
     * @throws IOException
     */
    private static void getBooks(ArrayList<BookEntity> songList) throws IOException {
        File file = new File("/Users/hx/Desktop/book.txt");
        BufferedReader bufferedReader;
        FileReader fileReader;
        fileReader = new FileReader(file);
        bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine()) != null){
            String[] tokens = line.split("/");
            BookEntity book = new BookEntity(tokens[0], tokens[1], tokens[2], tokens[3],tokens[4]
                    ,tokens[5],tokens[6],tokens[7],tokens[8],tokens[9]);
            songList.add(book);
        }
        fileReader.close();
        bufferedReader.close();
    }

}
